package Views;

import Controllers.SupplierCTR;
import Models.*;
import javax.swing.JOptionPane;

/**
 * @author Thomas & Erik
 */
public class SupplierInfoView extends javax.swing.JFrame
{
	private Supplier supplier;
	private SupplierCTR controller = new SupplierCTR();

    public SupplierInfoView(Supplier supplier)
	{
        this.supplier = supplier;
        initComponents();
		nameTF.setText(this.supplier.getName());
		adressTF.setText(this.supplier.getAddress());
		zipCodeSP.setValue(this.supplier.getZipCode());
		cityTF.setText(this.supplier.getCity());
		phoneNoSP.setValue(this.supplier.getPhoneNo());
		contactTF.setText(this.supplier.getContact());
		if(!supplier.isActivated())
		{
			activatedCB.setSelected(false);
		}
    }

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        descriptionLabel = new javax.swing.JLabel();
        activatedCB = new javax.swing.JCheckBox();
        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        adressLabel = new javax.swing.JLabel();
        adressTF = new javax.swing.JTextField();
        zipCodeLabel = new javax.swing.JLabel();
        zipCodeSP = new javax.swing.JSpinner();
        cityLabel = new javax.swing.JLabel();
        cityTF = new javax.swing.JTextField();
        phoneNoLabel = new javax.swing.JLabel();
        phoneNoSP = new javax.swing.JSpinner();
        contactLabel = new javax.swing.JLabel();
        contactTF = new javax.swing.JTextField();
        editButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Leveranførinformation");

        descriptionLabel.setText("Information om den valgte leverandør");

        activatedCB.setText("Aktiveret");
        activatedCB.setEnabled(false);

        nameLabel.setText("Firmanavn:");

        nameTF.setEnabled(false);

        adressLabel.setText("Adresse:");

        adressTF.setEnabled(false);

        zipCodeLabel.setText("PostNr:");

        zipCodeSP.setEnabled(false);

        cityLabel.setText("By:");

        cityTF.setEnabled(false);

        phoneNoLabel.setText("Telefon:");

        phoneNoSP.setEnabled(false);

        contactLabel.setText("Kontaktperson:");

        contactTF.setEnabled(false);

        editButton.setText("Rediger");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Luk");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Slet");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(descriptionLabel)
                        .add(18, 18, 18)
                        .add(activatedCB))
                    .add(layout.createSequentialGroup()
                        .add(editButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(removeButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 103, Short.MAX_VALUE)
                        .add(closeButton))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(nameLabel)
                            .add(6, 6, 6)
                            .add(nameTF))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(contactLabel)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(contactTF))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(adressLabel)
                                .add(zipCodeLabel)
                                .add(phoneNoLabel))
                            .add(6, 6, 6)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createSequentialGroup()
                                    .add(zipCodeSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 74, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(cityLabel)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                    .add(cityTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
                                .add(phoneNoSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 123, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(adressTF)))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(descriptionLabel)
                    .add(activatedCB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(contactLabel)
                    .add(contactTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(adressLabel)
                    .add(adressTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(zipCodeLabel)
                    .add(zipCodeSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cityLabel)
                    .add(cityTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(phoneNoSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(phoneNoLabel))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(editButton)
                    .add(removeButton)
                    .add(closeButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
		this.dispose();
	}//GEN-LAST:event_closeButtonActionPerformed

	private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
		int choice = JOptionPane.showConfirmDialog(this, "Er du sikker på at du vil slette "+supplier.getName()+"?", "Bekræftelse af sletning", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(choice == 0)
		{
			this.controller.removeSupplier(supplier);
			this.dispose();
		}
	}//GEN-LAST:event_removeButtonActionPerformed

	private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
		if (editButton.getText().equals("Rediger"))
		{
			nameTF.setEditable(true);
			adressTF.setEditable(true);
			zipCodeSP.setEnabled(true);
			cityTF.setEditable(true);
			phoneNoSP.setEnabled(true);
			contactTF.setEditable(true);
			editButton.setText("Gem");
			closeButton.setText("Annuller");
		}
		else if (editButton.getText().equals("Gem"))
		{
			this.supplier.setName(nameTF.getText());
			this.supplier.setAddress(adressTF.getText());
			this.supplier.setZipCode(Integer.parseInt(zipCodeSP.getValue().toString()));
			this.supplier.setCity(cityTF.getText());
			this.supplier.setPhoneNo(Integer.parseInt(phoneNoSP.getValue().toString()));
			this.supplier.setContact(contactTF.getText());
			nameTF.setEditable(false);
			adressTF.setEditable(false);
			zipCodeSP.setEnabled(false);
			cityTF.setEditable(false);
			phoneNoSP.setEnabled(false);
			contactTF.setEditable(false);
			editButton.setText("Rediger");
			closeButton.setText("Luk");
		}
	}//GEN-LAST:event_editButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox activatedCB;
    private javax.swing.JLabel adressLabel;
    private javax.swing.JTextField adressTF;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JTextField cityTF;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel contactLabel;
    private javax.swing.JTextField contactTF;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JLabel phoneNoLabel;
    private javax.swing.JSpinner phoneNoSP;
    private javax.swing.JButton removeButton;
    private javax.swing.JLabel zipCodeLabel;
    private javax.swing.JSpinner zipCodeSP;
    // End of variables declaration//GEN-END:variables

}
